package com.baozou.app.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnTouchListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.baozou.app.view.DragImageView;
import com.baozou.framework.util.AssetsUtil;

public class PicGalleryAdapter extends BaseAdapter {
	Context context;
	String[] picNames;
	AssetsUtil au;
	public PicGalleryAdapter(Context c){
		this.context = c;
		au = new AssetsUtil(c);
		picNames = au.getPicNames();
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return picNames.length;
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View view, ViewGroup arg2) {
		// TODO Auto-generated method stub
		if(view == null){
			final DragImageView iv = new DragImageView(context);
			try {
				iv.setImageBitmap(au.getBitmapFromAssets(picNames[position]));
				iv.setScaleType(ImageView.ScaleType.MATRIX);
				iv.setBackgroundColor(Color.WHITE);
				iv.setOnTouchListener(new OnTouchListener() {
					
					@Override
					public boolean onTouch(View v, MotionEvent e) {
						switch(e.getAction() & MotionEvent.ACTION_MASK){
							case MotionEvent.ACTION_DOWN:
								iv.down(e.getX(),e.getY());
								break;
							case MotionEvent.ACTION_UP:
								iv.up(e.getX(),e.getY());
								break;
							case MotionEvent.ACTION_MOVE:
								iv.drag(e.getX(),e.getY());
								break;
						}
						return false;
					}
				});
				view = iv;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return view;
	}

}
